/**
 * Copyright &copy; 2019-2022 <a href="https://gitee.com/rit/zx-pm">zx-pm</a> All rights reserved.
 */
package com.zx.pm.modules.zx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zx.pm.common.config.Global;
import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.common.web.BaseController;
import com.zx.pm.modules.sys.entity.Role;
import com.zx.pm.modules.sys.entity.User;
import com.zx.pm.modules.sys.utils.UserUtils;
import com.zx.pm.modules.zx.entity.Lzd;
import com.zx.pm.modules.zx.entity.MakeTender;
import com.zx.pm.modules.zx.entity.TenderFile;
import com.zx.pm.modules.zx.entity.TenderSection;
import com.zx.pm.modules.zx.service.LzdService;
import com.zx.pm.modules.zx.service.MakeTenderService;
import com.zx.pm.modules.zx.service.TenderFileService;
import com.zx.pm.modules.zx.service.TenderSectionService;

/**
 * 标书制作Controller
 * @author forest
 * @version 2019-02-11
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/makeTender")
public class MakeTenderController extends BaseController {

	@Autowired
	private MakeTenderService makeTenderService;
	
	@Autowired
	private LzdService lzdService;
	
	@Autowired
	private TenderFileService tenderFileService;
	
	@Autowired
	private TenderSectionService tenderSectionService;
	
	@ModelAttribute
	public MakeTender get(@RequestParam(required=false) String lzdId, @RequestParam(required=false) String sectionId) {
		MakeTender entity = new MakeTender();
		if (StringUtils.isNotBlank(lzdId)){
			entity.setLzdId(lzdId);
			entity.setSectionId(sectionId);
			entity = makeTenderService.get(entity);
		}
		if(entity == null) {
			entity = new MakeTender();
		}
		// 流转单ID
		entity.setLzd(lzdService.get(lzdId));
		return entity;
	}
	
	@RequiresPermissions("zx:makeTender:view")
	@RequestMapping(value = {"list", ""})
	public String list(MakeTender makeTender, HttpServletRequest request, HttpServletResponse response, Model model) {
		// 根据用户角色名称判断列表显示与隐藏
		User u = UserUtils.getUser();
		List<Role> roleList = u.getRoleList();
		if(roleList != null && roleList.size() > 0) {
			Role role = roleList.get(0);
			model.addAttribute("enname", role.getEnname());
		}
		Lzd lzd = makeTender.getLzd();
		if (lzd == null) {
			lzd = new Lzd();
		}
		lzd.setRemarks(makeTender.getRemarks());
		Page<Lzd> page = makeTenderService.findLzdPage(new Page<Lzd>(request, response), lzd);
		model.addAttribute("lzd", lzd);
		model.addAttribute("page", page);
		return "modules/zx/makeTenderList";
	}

	@RequiresPermissions("zx:audit:view")
	@RequestMapping(value = {"audit", ""})
	public String audit(MakeTender makeTender, HttpServletRequest request, HttpServletResponse response, Model model) {
		Lzd lzd = makeTender.getLzd();
		if (lzd == null) {
			lzd = new Lzd();
		}
		Page<Lzd> page = makeTenderService.findAuditList(new Page<Lzd>(request, response), lzd);
		model.addAttribute("lzd", lzd);
		model.addAttribute("page", page);
		return "modules/zx/tenderAuditList";
	}
	
	@RequiresPermissions("zx:makeTender:view")
	@RequestMapping(value = "form")
	public String form(MakeTender makeTender, Model model) {
		// 根据项目Id和标段ID获取已有的标书制作人和标书审核人
		MakeTender mt = makeTenderService.getMakeTenderUser(makeTender.getLzdId());
		// 如果标书制作人和标书审核人都为空，说明是第一次添加数据
		if(StringUtils.isBlank(makeTender.getMakeTenderBy())){
			if(mt != null && StringUtils.isNotBlank(mt.getMakeTenderBy())){
				makeTender.setMakeTenderBy(mt.getMakeTenderBy());
			}
		}
		
		if(StringUtils.isBlank(makeTender.getAuditor())){		
			if(mt != null && StringUtils.isNotBlank(mt.getAuditor())){
				makeTender.setAuditor(mt.getAuditor());	
			}
		}
		
		if(makeTender.getDepositMoney() == null) {
			if(mt != null && mt.getDepositMoney() != null) {
				makeTender.setDepositMoney(mt.getDepositMoney());
			}
		}
		TenderFile t = new TenderFile();
		t.setLzdId(makeTender.getLzdId());
		t.setMakeTenderId(makeTender.getId());
		List<TenderFile> fileList = tenderFileService.findList(t);
		int fileSize = 0;
		if(fileList != null && fileList.size() > 0) {
			fileSize = fileList.size();
		}
		for(int i = 0; i < 5 - fileSize; i ++) {
			fileList.add(new TenderFile());
		}
		TenderSection ts = tenderSectionService.get(makeTender.getSectionId());
		makeTender.setFileList(fileList);
		makeTender.setSection(ts);
		model.addAttribute("makeTender", makeTender);
		return "modules/zx/makeTenderForm";
	}
	
	
	@RequiresPermissions("zx:makeTender:view")
	@RequestMapping(value = "show")
	public String show(MakeTender makeTender, Model model) {
		TenderFile t = new TenderFile();
		t.setLzdId(makeTender.getLzdId());
		t.setMakeTenderId(makeTender.getId());
		List<TenderFile> fileList = tenderFileService.findList(t);
		int fileSize = 0;
		if(fileList != null && fileList.size() > 0) {
			fileSize = fileList.size();
		}
		for(int i = 0; i < 5 - fileSize; i ++) {
			fileList.add(new TenderFile());
		}
		makeTender.setFileList(fileList);
		model.addAttribute("mt", makeTender);
		return "modules/zx/makeTenderShow";
	}
	
	@RequiresPermissions("zx:audit:edit")
	@RequestMapping(value = "auditForm")
	public String auditForm(MakeTender makeTender, Model model) {
		TenderFile t = new TenderFile();
		t.setLzdId(makeTender.getLzdId());
		t.setMakeTenderId(makeTender.getId());
		makeTender.setFileList(tenderFileService.findList(t));
		model.addAttribute("makeTender", makeTender);
		return "modules/zx/tenderAuditForm";
	}

	
	@RequiresPermissions("zx:makeTender:edit")
	@RequestMapping(value = "save")
	public String save(MakeTender makeTender, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, makeTender)){
			return form(makeTender, model);
		}
		
		if(StringUtils.isBlank(makeTender.getTenderFlag())) {
			makeTender.setTenderFlag("0"); 
		}
		makeTenderService.save(makeTender);
		addMessage(redirectAttributes, "保存标书制作成功");
		return "redirect:"+Global.getAdminPath()+"/zx/makeTender/list?repage";
	}
	
	@RequiresPermissions("zx:makeTender:edit")
	@RequestMapping(value = "delete")
	public String delete(MakeTender makeTender, RedirectAttributes redirectAttributes) {
		makeTenderService.delete(makeTender);
		addMessage(redirectAttributes, "删除标书制作成功");
		return "redirect:"+Global.getAdminPath()+"/zx/makeTender/?repage";
	}

	
	@RequestMapping(value = "tenderAudit")
	public String tenderAudit(HttpServletRequest request, RedirectAttributes redirectAttributes, TenderFile tf) {
		tf.setId(request.getParameter("fileId"));
		makeTenderService.tenderAudit(tf);
		addMessage(redirectAttributes, "标书审核成功");
		return "redirect:"+Global.getAdminPath()+"/zx/makeTender/auditForm?lzdId=" + tf.getLzdId() + "&sectionId=" + tf.getSectionId() + "&repage";
	}
}